package Logika;

/**
 *
 * @author mejcu
 */

public class Tank
{
    private int     polozenieX=-1;
    private int     pociskX=-1;
    private int     polozenieY=-1;
    private int     pociskY=-1;
    private int     zwrot=-1;
    private short   life=1;
    private int     dx,dy;
    private int     whichInstance=-1;//scope<1...4>
    private boolean initialized=false;
    private int[] possibleMoves=new int[5];


    /**
     *
     */
    public Tank()
        {

        }
        /**
         *
         * @return
         */
        public int getLife(){return life;}

        /**
         *
         * @param polozenieX
         * @param polozenieY
         * @param direction
         */
        public Tank(int polozenieX, int polozenieY,int direction)
        {
                this.polozenieX = pociskX=polozenieX;
            	this.polozenieY = pociskY=polozenieY;
                this.life       = Zmienne.LIFE;
                this.zwrot      = direction;
                this.setDirection(zwrot);
                initialized=true;
                calculatePossibleMoves();
    	}
        /**
         *
         */
        public void additionalLife()
        {
            if(life<3)++this.life;
        }

        /**
         *
         * @param polozenieX
         * @param polozenieY
         * @param direction
         * @param whichInstance
         */
        public  Tank(int polozenieX, int polozenieY, int direction,int whichInstance)// Nie jest uzywany ten konstruktor !!!!
        {
            this.polozenieX=polozenieX;
            this.polozenieY=polozenieY;
            this.zwrot=direction;
            this.whichInstance=whichInstance;
            this.life=Zmienne.LIFE;
            this.setDirection(direction);
            this.initialized=true;
            this.calculatePossibleMoves();
        }


        /**
         *
         * @param polozenieX
         * @param polozenieY
         * @param direction
         * @param whichInstance
         */
        public void initializeNewTank(int polozenieX, int polozenieY, int direction,int whichInstance)
        {
            this.setX(polozenieX);
            this.setY(polozenieY);
            this.pobierzZwrot(direction);// tu bedzie jeszcze wywolane setDirection() calculatePossibleMove()
            this.whichInstance=whichInstance;
            this.life=Zmienne.LIFE;
            initialized=true;
        }


        /**
         *
         * @return
         */
        public boolean initialized()
        {
            return initialized;
        }

        /**
         *
         * @return
         */
        public int zwrocPolozenieX()
    	{
                return polozenieX;
    	}

        /**
         *
         * @return
         */
        public int[] getCoords(){
         int temp[]=new int[3];
         temp[0]=polozenieX;
         temp[1]=polozenieY;
         temp[2]=zwrot;
         return temp;
        }

        /**
         *
         * @return
         */
        public int zwrocPolozenieY()
        {
                return polozenieY;
    	}

        /**
         *
         * @return
         */
        public int zwrocKierunek()
    	{
                return zwrot;
    	}

        /**
         *
         * @param x
         */
        public void setX(int x)
        {
            polozenieX=x;
        }

        /**
         *
         * @param y
         */
        public void setY(int y)
        {
            polozenieY=y;
        }


        /**
         *
         * @param direction
         */
        public void pobierzZwrot(int direction)// Ta ,etoda jest zawsze jako ostatnia wywolywana przy updateTank
    	{
                zwrot=direction;
                this.setDirection(direction);
                calculatePossibleMoves();
        }

        /**
         *
         * @param x
         */
        public void pobierzX(int x)
    	{
                polozenieX+=x;
                pociskX+=polozenieX;
    	}

        /**
         *
         * @param y
         */
        public void pobierzY(int y)
    	{
                polozenieY+=y;
                pociskY+=polozenieY;
    	}

        /**
         *
         * @return
         */
        public boolean isAlive()
        {
                return this.life>1;
        }

        /**
         *
         */
        public void shot()
        {
            --this.life;
        }

    	private void setDirection(int direction)
    	{
            dx=dy=0;
        	switch(direction)
        	{
                case Zmienne.TURN_NORTH:
                    dx=-1;
                break;
            	case Zmienne.TURN_EAST:
                	dy=1;
                break;
                case Zmienne.TURN_SOUTH:
                	dx=1;
                break;
                case Zmienne.TURN_WEST:
                    dy=-1;
                break;
        	}
    	}

        /**
         *
         * @return
         */
        public int getDX()
        {
            return dx;
        }

        /**
         * 
         * @return
         */
        public int getDY()
        {
            return dy;
        }


        /**
         *
         */
        public void calculatePossibleMoves()
        {
            possibleMoves[0]=10*polozenieX+polozenieY;
            possibleMoves[1]=10*(polozenieX+Zmienne.NORTH[0])+(polozenieY+Zmienne.NORTH[1]);
            possibleMoves[2]=10*(polozenieX+Zmienne.EAST[0])+(polozenieY+Zmienne.EAST[1]);
            possibleMoves[3]=10*(polozenieX+Zmienne.SOUTH[0])+(polozenieY+Zmienne.SOUTH[1]);
            possibleMoves[4]=10*(polozenieX+Zmienne.WEST[0])+(polozenieY+Zmienne.WEST[1]);

        }

        /**
         *
         * @param coordinate
         * @return
         */
        public boolean ifContains(int coordinate)
        {
            for(int i=0; i< 5; i++)
            {
                if(possibleMoves[i]==coordinate)
                    return true;
            }
            return false;
        }

    @Override
        public String toString()
        {
            return "Polozenie "+polozenieX+", "+polozenieY+", "+zwrot+" "+whichInstance+"\n";
        }
}
